/**
  ******************************************************************************
  * File Name          : envirctrl.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "network.h"
#include "route.h"

static uint8_t route_gw_up_addtp(Route_PayloadType *rtpl)
{
  uint8_t index;
  
  if(rtpl->pl_len != sizeof(Route_pl_un_msg_Type))
  {
    return 1;
  }
  
  index = ((Route_pl_un_msg_Type *)(rtpl->payload))->msg;
  
	if(index > Route_BoundList.num)
	{
		return 2;
	}
	
  snprintf(Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN,
          "{\"id\":\"3\",\"version\":\""ONEJSON_VERSION"\",\"params\":{\"deviceName\":\"%s\",\"productID\":\"%s\",\"sasToken\":\"%s\"}}", 
          Route_BoundList.item[index].did, 
          Route_str_pids[Route_DidToType(Route_BoundList.item[index].did)],
          Route_BoundList.item[index].token);
	
  snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Topology_Adddev, ROUTE_SELF_DID);

  return 0;
}

static uint8_t route_gw_up_deltp(Route_PayloadType *rtpl)
{
  uint8_t index;
  
  if(rtpl->pl_len != sizeof(Route_pl_un_msg_Type))
  {
    return 1;
  }
  
  index = ((Route_pl_un_msg_Type *)(rtpl->payload))->msg;
  
	if(index > Route_UnboundList.num)
	{
		return 2;
	}
	
  snprintf(Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN,
          "{\"id\":\"4\",\"version\":\""ONEJSON_VERSION"\",\"params\":{\"deviceName\":\"%s\",\"productID\":\"%s\",\"sasToken\":\"%s\"}}", 
          Route_UnboundList.item[index].did, 
          Route_str_pids[Route_DidToType(Route_UnboundList.item[index].did)],
          Route_UnboundList.item[index].token);

  snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Topology_Deldev, ROUTE_SELF_DID);

  return 0;
}

static uint8_t route_gw_up_gettp(void)
{
  strncpy(Network_Pub_payload, "{\"id\":\"2\",\"version\":\""ONEJSON_VERSION"\"}", NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN);
  snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Topology_Getdev, ROUTE_SELF_DID);

  return 0;
}

static char str_buf[252];

static uint8_t route_gw_up_sblx(void)
{
  uint8_t index;
  uint8_t pos;
  Route_DidFormatType didst;
  
  memset(str_buf, 0, sizeof(str_buf));
  memset(&Route_LocalTopoList, 0, sizeof(Route_ToPoListType));
  
  Route_ConvertToTopolist(&Route_LocalTopoList);
  
  pos = 0;
  for(index=0; index<Route_LocalTopoList.topo_num; index++)
  {
    if(Route_LocalTopoList.dids[0] == 0)
    {
      continue;
    }
    
    if(Route_ParseDID(Route_LocalTopoList.dids[index], &didst) != 0)
    {
      continue;
    }
    
    pos += snprintf(str_buf+pos, 20, "%d-%s,", didst.type, Route_LocalTopoList.dids[index]);
  }
  
  snprintf(Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, \
           "{\"id\":\"5\",\"version\":\""ONEJSON_VERSION"\",\"params\":{\"sblx\":{\"value\":\"%s\"}}}", str_buf);
  snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Property_Post, ROUTE_SELF_DID);

  return 0;
}

static uint8_t route_gw_up_time(void)
{
#if (COLLECTOR_USE_TYPE == COLLECTOR_USE_NTC)
	snprintf(Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, \
          "{\"id\":\"6\",\"version\":\""ONEJSON_VERSION"\",\"params\":{\"time\":{\"value\":1},\"ver\":{\"value\":\"%s-NTC-%s-%s-0x%X\"}}}", \
          Sys_Version.ver, Sys_Version.vfd_ver, Sys_Version.bdata, RCU_RSTSCK);
#elif (COLLECTOR_USE_TYPE == COLLECTOR_USE_LORA)
	snprintf(Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, \
          "{\"id\":\"6\",\"version\":\""ONEJSON_VERSION"\",\"params\":{\"time\":{\"value\":1},\"ver\":{\"value\":\"%s-LoRa-%s-%s-0x%X\"}}}", \
          Sys_Version.ver, Sys_Version.vfd_ver, Sys_Version.bdata, RCU_RSTSCK);
#else
	#error must define COLLECTOR_USE_TYPE in bsp.h.
#endif
  
  snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Property_Post, ROUTE_SELF_DID);
  
  RCU_RSTSCK |= RCU_RSTSCK_RSTFC;
  
  return 0;
}

extern Cat1_StaticInfoType  Cat1_StaticInfo;
static uint8_t route_gw_up_cat1info(void)
{
  snprintf(Network_Pub_payload, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN, \
          "{\"id\":\"7\",\"version\":\""ONEJSON_VERSION"\",\"params\":{\"cat1_iccid\":{\"value\":\"%s\"},\"cat1_imei\":{\"value\":\"%s\"},\"cat1_sn\":{\"value\":\"%s\"},\"cat1_ver\":{\"value\":\"%s\"}}}", \
          Cat1_StaticInfo.iccid, Cat1_StaticInfo.imei, Cat1_StaticInfo.sn, Cat1_StaticInfo.ver);
  snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Property_Post, ROUTE_SELF_DID);
  
  return 0;
}

const static char power_off[] = "{\"id\":\"8\",\"version\":\""ONEJSON_VERSION"\",\"params\":{\"pwr\":{\"value\":\"1\"}}}";
static uint8_t route_gw_up_power(void)
{    
  strncpy(Network_Pub_payload, power_off, NETWORK_PUB_PAYLOAD_BUFFER_MAXLEN);
  snprintf(Network_Pub_topic, NETWORK_PUB_TOPIC_BUFFER_MAXLEN, Route_str_Pub_Property_Post, ROUTE_SELF_DID);
  
  return 0;
}

uint8_t Route_gw_up(Route_PayloadType *rtpl)
{
  uint8_t err = 1;
  
  switch(rtpl->pl_type)
  {
    case ROUTE_PLTYP_GW_ADDTP:
			err = route_gw_up_addtp(rtpl);
      break;
    
    case ROUTE_PLTYP_GW_DELTP:
      err = route_gw_up_deltp(rtpl);
      break;
    
    case ROUTE_PLTYP_GW_GETTP:
      err = route_gw_up_gettp();
      break;
    
    case ROUTE_PLTYP_GW_SBLX:
      err = route_gw_up_sblx();
      break;
    
    case ROUTE_PLTYP_GW_TIME:
      err = route_gw_up_time();
      break;
    
    case ROUTE_PLTYP_GW_POWER:
      err = route_gw_up_power();
      break;
    
    case ROUTE_PLTYP_GW_CAT1INFO:
      err = route_gw_up_cat1info();
      break;
    
    default:
      err = 1;
      break;
  }
  
  return err;
}
